package cn.sfinger.huifu.p2p.integration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.sfinger.huifu.p2p.base.HuifuConfig;
import cn.sfinger.huifu.p2p.base.HuifuConstant;
import cn.sfinger.huifu.p2p.base.HuifuException;
import cn.sfinger.huifu.p2p.util.HttpClientUtil;
import cn.sfinger.huifu.p2p.util.HuifuUtil;

@Component
public class HuifuIntegration{
	private static final Logger log = LoggerFactory.getLogger(HuifuIntegration.class);
	
	@Autowired
	private HuifuConfig huifuConfig;
		
	/**
	 * 4.2.1.	用户开户
	 * 
	 * @param bgRetUrl
	 * @param retUrl
	 * @param usrId
	 * @param usrName
	 * @param idType
	 * @param idNo
	 * @param usrMp
	 * @param usrEmail
	 * @param merPriv
	 * @param charSet
	 * @return
	 */
	public Map<String, Object> userRegister(String bgRetUrl, String retUrl,
			String usrId, String usrName, String idType, String idNo, String usrMp,
			String usrEmail, String merPriv, String charSet){
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_USER_REGISTER);
		params.put("MerCustId", this.huifuConfig.getMerId());
		params.put("BgRetUrl", bgRetUrl);
		params.put("UsrId", usrId);
		params.put("RetUrl", retUrl);
		params.put("UsrName", usrName);
		params.put("IdType", idType);
		params.put("IdNo", idNo);
		params.put("UsrMp", usrMp);
		params.put("UsrEmail", usrEmail);
		params.put("MerPriv", merPriv);
		params.put("CharSet", charSet);
		
		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KYES_USER_REGISTER, huifuConfig.getPrivateKey());
		
		// 返回请求参数列表
		return params;
	}
	
	/**
	 * 4.2.2.	用户绑卡接口
	 * 
	 * @param usrCustId
	 * @param bgRetUrl
	 * @param merPriv
	 * @return
	 */
	public Map<String, Object> userBindCard(String usrCustId, String bgRetUrl, String merPriv){
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_USER_BIND_CARD);
		params.put("MerCustId", this.huifuConfig.getMerId());
		params.put("UsrCustId", usrCustId);
		params.put("BgRetUrl", bgRetUrl);
		params.put("MerProv", merPriv);
		
		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_USER_BIND_CARD, huifuConfig.getPrivateKey());
		
		// 返回请求参数列表
		return params;	
	}

	/**
	 * 4.2.3.	用户登录
	 * 
	 * @param usrCustId
	 * @return
	 */
	public Map<String, Object> userLogin(String usrCustId){ 
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_USER_LOGIN);
		params.put("MerCustId", this.huifuConfig.getMerId());
		params.put("UsrCustId", usrCustId);
		
		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_USER_BIND_CARD, huifuConfig.getPrivateKey());
		
		// 返回请求参数列表
		return params;
	}
	
	/**
	 * 4.3.11.	自动扣款转账（商户用）页面
	 * 
	 * @param ordId
	 * @param outAcctId
	 * @param transAmt
	 * @param inCustId
	 * @param inAcctId
	 * @param bgRetUrl
	 * @param merPriv
	 * @return
	 * @throws HuifuException
	 */
	public Map<String, Object> transfer(String ordId, String outAcctId, String transAmt,String inCustId, String inAcctId, String bgRetUrl, String merPriv ) throws HuifuException{
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_TRANSFER);
		params.put("OrdId", ordId);
		params.put("OutCustId", this.huifuConfig.getMerId());
		params.put("OutAcctId", outAcctId);
		params.put("TransAmt", transAmt);
		params.put("InCustId", inCustId);
		params.put("InAcctId", inAcctId);
		params.put("RetUrl", null);
		params.put("BgRetUrl", bgRetUrl);
		params.put("MerPriv", merPriv);
		
		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_TRANSFER, huifuConfig.getPrivateKey());
		
		// 返回请求参数列表
		return params;	
	}

	
	/**
	 * 4.4.1.	余额查询（页面）
	 * 
	 * @param usrCustId
	 * @return
	 */	
	public Map<String, Object> queryBalance(String usrCustId){
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_QUERY_BALANCE);
		params.put("MerCustId", this.huifuConfig.getMerId());
		params.put("UsrCustId", usrCustId);

		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_QUERY_BALANCE, huifuConfig.getPrivateKey());
				
		// 返回请求参数列表
		return params;			
	}

	/**
	 * 4.4.2.	余额查询（后台）
	 * 
	 * @param usrCustId
	 * @return
	 * @throws HuifuException
	 */
	public JSONObject queryBalanceBg(String usrCustId) throws HuifuException{
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_QUERY_BALANCE_BG);
		params.put("MerCustId", this.huifuConfig.getMerId());
		params.put("UsrCustId", usrCustId);
		
		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_QUERY_BALANCE_BG, huifuConfig.getPrivateKey());
		
		// 后台接口请求发起,传递报文
		return HttpClientUtil.getInstantce().getPostResponseAsJSON(huifuConfig.getLocation(), params);
	}
		
	/**
	 * 4.4.3.	商户子账户信息查询
	 * @return
	 * @throws HuifuException
	 * @throws IOException 
	 */
	public JSONObject querySubAccounts() throws HuifuException{
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_QUERY_ACCTS);
		params.put("MerCustId", huifuConfig.getMerId());
		
		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_QUERY_ACCT, huifuConfig.getPrivateKey());
		
		// 后台接口请求发起,传递报文
		return HttpClientUtil.getInstantce().getPostResponseAsJSON(huifuConfig.getLocation(), params);
	}
	
	
	/**
	 * 4.3.16.	前台用户间转账接口
	 *  
	 * @param ordId
	 * @param usrCustId
	 * @param transAmt
	 * @param inUsrCustId
	 * @param retUrl
	 * @param bgRetUrl
	 * @param merPriv
	 * @param reqExt
	 * @return
	 */
	public Map<String, Object> usrTransfer(String ordId, String usrCustId, String transAmt, String inUsrCustId,
			String retUrl, String bgRetUrl, String merPriv, String reqExt){
		// 按照文档要求组装参数
		Map<String, Object> params= new HashMap<String, Object>();
		params.put("Version", HuifuConstant.VERSION_10);
		params.put("CmdId", HuifuConstant.CMDID_USR_TRANSFER);
		params.put("MerCustId", this.huifuConfig.getMerId());
		params.put("UsrCustId", usrCustId);
		params.put("OrdId", ordId);
		params.put("RetUrl", retUrl);
		params.put("BgRetUrl", bgRetUrl);
		params.put("InUsrCustId", inUsrCustId);
		params.put("TransAmt", transAmt);
		params.put("MerPriv", merPriv);
		params.put("ReqExt", reqExt);

		// 获取参数列表，进行签名
		HuifuUtil.sign(params, HuifuConstant.SIGN_KEYS_USR_TRANSFER, huifuConfig.getPrivateKey());
		
		// 返回请求参数列表
		return params;	
	}

	public HuifuConfig getHuifuConfig() {
		return huifuConfig;
	}

	public void setHuifuConfig(HuifuConfig huifuConfig) {
		this.huifuConfig = huifuConfig;
	}
}